{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using exhaustive enumeration to find the cube root "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 is not a perfect cube\n"
     ]
    }
   ],
   "source": [
    "x = int(7)\n",
    "ans = 0\n",
    "while ans ** 3 < abs(x):\n",
    "    ans += 1\n",
    "if ans ** 3 != abs(x):\n",
    "    print(x, 'is not a perfect cube')\n",
    "else:\n",
    "    if x < 0:\n",
    "        ans -= ans\n",
    "    print('cube root of ', x, 'is', ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using exhaustive enumeration to test primality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 is a prime number\n"
     ]
    }
   ],
   "source": [
    "x = int(7)\n",
    "smallest_divisor = None\n",
    "for guess in range(2, x):\n",
    "    if x % guess == 0:\n",
    "        smallest_divisor = guess\n",
    "        break\n",
    "if smallest_divisor != None:\n",
    "    print('the smallest divisor of', x, 'is', smallest_divisor)\n",
    "else:\n",
    "    print(x, 'is a prime number')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A more efficient primality test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 is a prime number\n"
     ]
    }
   ],
   "source": [
    "# enter an integer greater than 2\n",
    "x = int(7)\n",
    "smallest_divisor = None\n",
    "if x % 2 == 0:\n",
    "    smallest_divisor = 2\n",
    "else:\n",
    "    for guess in range(3, x, 2):\n",
    "        if x % guess == 0:\n",
    "            smallest_divisor = guess\n",
    "            break\n",
    "if smallest_divisor != None:\n",
    "    print('the smallest divisor of', x, 'is', smallest_divisor)\n",
    "else:\n",
    "    print(x, 'is a prime number')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
